/**
 * Copyright (C), 2020-2020, 软件公司
 * FileName: ExecutionQuery.java
 * Author:   cakin
 * Date:     2020/4/25
 * Description: 执行流查询
 */
package com.syc.activiti.chapter9;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ProcessInstance;

/**
 * @className: ExecutionQuery
 * @description: 执行流查询
 * @date: 2020/4/25
 * @author: cakin
 */
@Slf4j
public class ExecutionQuery {
    /**
     * 数字常量
     */
    private static final int NUM8 = 8;
    /**
     * 数字常量
     */
    private static final int NUM5 = 5;
    /**
     * 数字常量
     */
    private static final int NUM7 = 7;
    /**
     * 数字常量
     */
    private static final int NUM6 = 6;

    public static void main( String[] args ) {
        // 创建流程引擎
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // 得到流程存储服务实例
        RepositoryService repositoryService = engine.getRepositoryService();
        // 得到运行时服务组件
        RuntimeService runtimeService = engine.getRuntimeService();
        // 部署流程描述文件
        repositoryService.createDeployment()
                .addClasspathResource("bpmn/chapter9/ExecutionQuery.bpmn").deploy();
        //设置参数
        Map<String, Object> vars1 = new HashMap<>();
        vars1.put("days", NUM5);
        Map<String, Object> vars2 = new HashMap<>();
        vars2.put("days", NUM6);
        Map<String, Object> vars3 = new HashMap<>();
        vars3.put("days", NUM7);
        vars3.put("name", "crazyit");
        /***********************************************************
         **********************************************************/
        // 开始流流程
        ProcessInstance pi1 = runtimeService.startProcessInstanceByKey("testProcess",
                "businessKey1", vars1);
        ProcessInstance pi2 = runtimeService.startProcessInstanceByKey("testProcess",
                "businessKey2", vars2);
        ProcessInstance pi3 = runtimeService.startProcessInstanceByKey("testProcess",
                "businessKey3", vars3);
        /***********************************************************
         **********************************************************/
        // 使用执行流查询方法
        List<Execution> exes = runtimeService.createExecutionQuery()
                .processDefinitionKey("testProcess").list();
        log.info("使用processDefinitionKey方法查询执行流：" + exes.size());
        exes = runtimeService.createExecutionQuery()
                .processInstanceBusinessKey("businessKey1").list();
        log.info("使用processInstanceBusinessKey方法查询执行流：" + exes.size());
        exes = runtimeService.createExecutionQuery()
                .messageEventSubscriptionName("messageName").list();
        log.info("使用messageEventSubscriptionName方法查询执行流：" + exes.size());
        // 根据节点id属性查询当前的执行流
        Execution exe = runtimeService.createExecutionQuery()
                .activityId("messageintermediatecatchevent1")
                .processInstanceId(pi1.getId()).singleResult();
        log.info("使用activityId和processInstanceId方法查询执行流，得到执行ID：" + exe.getId());
        //让流程往下执行
        runtimeService.messageEventReceived("messageName", exe.getId());
        /***********************************************************
         **********************************************************/
        exes = runtimeService.createExecutionQuery().signalEventSubscriptionName("signalName").list();
        log.info("使用signalEventSubscriptionName方法查询执行流：" + exes.size());
        // 根据参数查询执行流
        exes = runtimeService.createExecutionQuery().variableValueEquals("name", "crazyit").list();
        log.info("使用variableValueEquals方法查询执行流：" + exes.size());
        exes = runtimeService.createExecutionQuery().variableValueGreaterThan("days", NUM5).list();
        log.info("使用variableValueGreaterThan方法查询执行流：" + exes.size());
        exes = runtimeService.createExecutionQuery().variableValueGreaterThanOrEqual("days", NUM5).list();
        log.info("使用variableValueGreaterThanOrEqual方法查询执行流：" + exes.size());
        exes = runtimeService.createExecutionQuery().variableValueLessThan("days", NUM6).list();
        log.info("使用variableValueLessThan方法查询执行流：" + exes.size());
        exes = runtimeService.createExecutionQuery().variableValueLessThanOrEqual("days", NUM6).list();
        log.info("使用variableValueLessThanOrEqual方法查询执行流：" + exes.size());
        exes = runtimeService.createExecutionQuery().variableValueLike("name", "%crazy%").list();
        log.info("使用variableValueLike方法查询执行流：" + exes.size());
        exes = runtimeService.createExecutionQuery().variableValueNotEquals("days", NUM8).list();
        log.info("使用variableValueNotEquals方法查询执行流：" + exes.size());
    }
}
